package com.wanxi.dangdang.service.details;


import com.wanxi.dangdang.model.pojo.Permission;
import com.wanxi.dangdang.model.vo.AdminVo;
import com.wanxi.dangdang.service.AdminService;
import com.wanxi.dangdang.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service("selfUserDetailsServiceImplA")
public class SelfUserDetailsServiceImplA implements UserDetailsService {

    @Autowired
    private AdminService adminService;

    @Autowired
    private PermissionService permissionService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        AdminVo admin = adminService.loadUserByUsername(username);
        if (ObjectUtils.isEmpty(admin)) {
            throw new UsernameNotFoundException("根据用户名未找到用户信息");
        }
        List<Permission> permissions = permissionService.getPermissionsByUserId(admin.getId());
        Set set = new HashSet(permissions);
        admin.setAuthorities(set);
        return admin;
    }
}
